import CryptoJS from "crypto-js";
import NodeRSA from 'node-rsa';

/**
 * ¼ÓÃÜ
 */
class DataUtil{
    constructor() {
        this.AES_KEY_SALT = C("security.aes");
        this.AES_KEY = CryptoJS.enc.Utf8.parse(this.AES_KEY_SALT);
        this.DES_REQUEST_PRIVATE_KEY = C("security.des-request.private");
        this.DES_RESPONSE_PUBLIC_KEY = C("security.des-response.public");

        /**
         * ¶ÔÍâ¿ª·ÅµÄkey
         */
        this.DES_REQUEST_PUBLIC_KEY = C("security.des-request.public");
        this.DES_RESPONSE_PRIVATE_KEY = C("security.des-response.private");
    }

    /**
     * AES¼ÓÃÜ
     * @param password
     * @returns {string}
     */
    AESEncrypt(password){
        let encryptedData  = CryptoJS.AES.encrypt(password, this.AES_KEY, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return encryptedData.ciphertext.toString();
    }

    /**
     * AES¼ÓÃÜ
     * @param password
     * @returns {string}
     */
    AESDecrypt(data){
        let encryptedHexStr  = CryptoJS.enc.Hex.parse(data);
        let encryptedBase64Str  = CryptoJS.enc.Base64.stringify(encryptedHexStr);
        let decryptedData  = CryptoJS.AES.decrypt(encryptedBase64Str, this.AES_KEY, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        return decryptedData.toString(CryptoJS.enc.Utf8);
    }

    /**
     * RSA¹«Ô¿¼ÓÃÜ
     * @param data
     * @returns {string|Buffer}
     */
    RSAEncrypt(data, publicKey) {
        const nodersa = new NodeRSA(publicKey, 'pkcs8-public-pem');
        // nodersa.setOptions({encryptionScheme: 'pkcs1', signingScheme: 'pkcs1'})
        nodersa.setOptions({encryptionScheme: 'pkcs1'})
        const encrypted = nodersa.encrypt(data, 'base64');
        return encrypted;
    }

    /**
     * RSAË½Ô¿½âÃÜ
     * @param data
     * @returns {Buffer|Object|string}
     * @constructor
     */
    RSADecrypt(data, privateKey) {
        const nodersa = new NodeRSA(privateKey, 'pkcs8-private-pem');
        // nodersa.setOptions({encryptionScheme: 'pkcs1', signingScheme: 'pkcs1'})
        nodersa.setOptions({encryptionScheme: 'pkcs1'})
        return  nodersa.decrypt(data, 'utf8');
    }


    /**
     * RSA¹«Ô¿¼ÓÃÜ
     * @param data
     * @returns {string|Buffer}
     */
    RequestRSAEncrypt(data) {
        return this.RSAEncrypt(data, this.DES_REQUEST_PUBLIC_KEY)
    }

    /**
     * RSAË½Ô¿½âÃÜ
     * @param data
     * @returns {Buffer|Object|string}
     * @constructor
     */
    RequestRSADecrypt(data) {
        return this.RSADecrypt(data, this.DES_REQUEST_PRIVATE_KEY)
    }

    /**
     * RSA¹«Ô¿¼ÓÃÜ
     * @param data
     * @returns {string|Buffer}
     */
    ResponseRSAEncrypt(data) {
        return this.RSAEncrypt(data, this.DES_RESPONSE_PUBLIC_KEY)
    }

    /**
     * RSAË½Ô¿½âÃÜ
     * @param data
     * @returns {Buffer|Object|string}
     * @constructor
     */
    ResponseRSADecrypt(data) {
        return this.RSADecrypt(data, this.DES_RESPONSE_PRIVATE_KEY)
    }
}

let ref = new DataUtil();
export default ref;